﻿using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace NLBS.Utility
{
    public class UnZipClass
    {
        public UnZipClass() { }

        private string _savePath;
       
        public string SavePath
        {
            get { return _savePath; }
            set { _savePath = value; }
        }

        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="args">包含要解压的文件名和要解压到的目录名数组</param>
        public void UnZip(string[] args)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
            try
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(args[1]);

                    string filePath=args[1]+theEntry.Name;
                    string fileName = Path.GetFileName(theEntry.Name);

                    //生成解压目录
                    Directory.CreateDirectory(directoryName);
                    if (fileName == string.Empty)
                    {
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }
                    }
                    else
                    {

                        //解压文件到指定的目录

                        FileStream streamWriter = File.Create(filePath);

                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }

                        streamWriter.Close();
                    }

                }
                s.Close();
            }
            catch (Exception eu)
            {
                throw eu;
            }
            finally
            {
                s.Close();
            }

        }//end UnZip

        public static bool UnZipFile(string file, string dir)
        {
            try
            {
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);
                string fileFullName = Path.Combine(dir, file);
                ZipInputStream s = new ZipInputStream(File.OpenRead(fileFullName));

                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);

                    if (directoryName != String.Empty)
                        Directory.CreateDirectory(Path.Combine(dir, directoryName));

                    if (fileName != String.Empty)
                    {
                        FileStream streamWriter = File.Create(Path.Combine(dir, theEntry.Name));
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }

                        streamWriter.Close();
                    }
                }
                s.Close();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

    }//end UnZipClass
}
